package com.daka.controller;

import com.daka.entity.Volunteer;
import com.daka.enumerate.StatusCode;
import com.daka.service.VolunteerService;
import com.daka.tools.StrUtils;
import com.daka.tools.Page;
import com.daka.tools.excel.ExcelUtils;
import com.daka.vo.request.VolunteerRequestVo;
import com.daka.vo.response.BaseResponseVo;
import com.daka.vo.response.DataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by yangx
 * Date: 2016/8/1 0001
 * Time: 11:05
 */

@Controller
@RequestMapping("/volunteer")
@Slf4j
public class VolunteerController {

    @Autowired
    private VolunteerService volunteerService;

    /**
     * 组织下的志愿者列表
     * @param vo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryList")
    public DataResponseVo queryListByGroup(VolunteerRequestVo vo) {
        System.out.println(vo.toString());
        if(vo.getGroupId()==null){
            return new DataResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
        }
        System.out.println(vo.getServiceField().length);
        //？
        if((vo.getSkill()!=null && vo.getSkill().length<1)||(vo.getJob()!=null && vo.getJob().length<1)||(vo.getServiceField()!=null && vo.getServiceField().length<1)){
            return new DataResponseVo(StatusCode.PARAMETER_INVALID.getCode(),StatusCode.PARAMETER_INVALID.getMessage());
        }
        Page<Volunteer> list = volunteerService.selectByGroupId(vo);

        return new DataResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage() ,list);
    }

    /**
     * 志愿者详情  memberCode
     * @param volunteer
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDetail")
    public BaseResponseVo queryDetail(Volunteer volunteer) {
        if(volunteer.getMemberCode()==null){
            return new BaseResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
        }
        volunteer = volunteerService.queryByEntity(volunteer);
        return new DataResponseVo<Volunteer>(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(),volunteer);
    }

    /**
     * 修改信息 id必传
     * @param volunteer
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateInfo")
    public BaseResponseVo updateUserInfo( Volunteer volunteer) {
        try {
            if (StrUtils.isNotEmpty(volunteer.getSkill())) {
                volunteer.setSkill(volunteer.getSkill().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(volunteer.getJob())) {
                volunteer.setSkill(volunteer.getJob().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(volunteer.getServiceField())) {
                volunteer.setServiceField(volunteer.getServiceField().replace(",", ";"));
            }
            if (StrUtils.isNotEmpty(volunteer.getProvince()) && StrUtils.isNotEmpty(volunteer.getCity()) && StrUtils.isNotEmpty(volunteer.getDistrict())) {
                volunteer.setResidenceAddress(volunteer.getProvince() + "-" + volunteer.getCity() + "-" + volunteer.getDistrict());
            }
            if (StrUtils.isNotEmpty(volunteer.getCardno())) {
                volunteer.setBirthDay(volunteer.getCardno().substring(6, 10) + "-" + volunteer.getCardno().substring(10, 12) + "-" + volunteer.getCardno().substring(12, 14));
            }
            int result = volunteerService.update(volunteer);
            if (result == 0) {
                return new BaseResponseVo(StatusCode.ERROR.getCode(), StatusCode.ERROR.getMessage());
            }
            return new BaseResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponseVo(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }

    /**
     * /volunteer/excel/groupId=60
     * @param vo
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/excel")
    public DataResponseVo excel(VolunteerRequestVo vo,HttpServletRequest request, HttpServletResponse response) {
        try {
            System.out.println(vo);
            if (vo.getGroupId() == 0) {
                return new DataResponseVo(StatusCode.PARAMETER_INVALID.getCode(), StatusCode.PARAMETER_INVALID.getMessage());
            }
            List<Volunteer> list = volunteerService.queryByGroupId(vo);

            String[] Title = new String[]{"姓名", "手机号", "性别", "服务领域", "职业", "技能", "证件类别", "证件号"};// 表头
            String[] includes = new String[]{"realName", "mobile", "sex", "serviceField", "job", "skill", "cardType", "cardno"};

            String result = ExcelUtils.exportExcel(response, "志愿者列表", Title, list, includes);

            return new DataResponseVo(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), result);
        }catch (Exception e){
            e.printStackTrace();
            return new DataResponseVo(StatusCode.SYS_ERROR.getCode(), StatusCode.SYS_ERROR.getMessage());
        }
    }


}







